<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  header("location:checklogin.php");
}

$branchArray = array();
if(isset($_POST['submit']))
{  
	 $userType       = (isset($_POST['userType'])) && ($_POST['userType'] != "") ? $_POST['userType'] : "" ;
	 $userName       = (isset($_POST['userName'])) && ($_POST['userName'] != "") ? $_POST['userName'] : "" ;
	 $password       = (isset($_POST['password'])) && ($_POST['password'] != "") ? $_POST['password'] : "" ;
	 $branchId       = (isset($_POST['branchId'])) && ($_POST['branchId'] != "") ? $_POST['branchId'] : 0 ;

   $insertUsers = "INSERT INTO user (userName,password,userType,branchId)
                             VALUES('".$userName."',
                                   '".$password."',
                                   '".$userType."',
                            			  ".$branchId.")";
   $insertUsersResult = mysql_query($insertUsers);
   if(!$insertUsersResult)
     die("Insert Query Not Inserted : ".mysql_error());
   else
   {
     //header("Location:users.php");
   }
}
 $i=0;
 $selectBranch = "SELECT  userId,userName,password,userType,branchName
                         FROM user
                        LEFT JOIN branch ON branch.branchId =  user.branchId";
$selectBranchResult = mysql_query($selectBranch);
while($branchRow = mysql_fetch_array($selectBranchResult))
{
  $branchArray[$i]['userId']      = $branchRow['userId'];
  $branchArray[$i]['userName']    = $branchRow['userName'];
  $branchArray[$i]['password']    = $branchRow['password'];
  $branchArray[$i]['userType']    = $branchRow['userType'];
  $branchArray[$i]['branchName']  = $branchRow['branchName'];
  $i++;
}

 $k=0;
 $branchSubArray = array();
 $selectSubBranch = "SELECT  branchId,branchName
                       FROM branch
                      ORDER BY branchName";
$selectSubBranchRes = mysql_query($selectSubBranch);
while($branchRow = mysql_fetch_array($selectSubBranchRes))
{
  $branchSubArray['branchId'][$k]    = $branchRow['branchId'];
  $branchSubArray['branchName'][$k]  = $branchRow['branchName'];
  $k++;
}


$userArrayVal[0]    = "Super Admin";
$userArrayOutput[0] = "Super Admin";
$userArrayVal[1]    = "Admin";
$userArrayOutput[1] = "Admin";

include("./bottom.php");
$smarty->assign("branchArray",$branchArray);  
$smarty->assign("userArrayOutput",$userArrayOutput);  
$smarty->assign("branchSubArray",$branchSubArray);  
$smarty->assign("userArrayVal",$userArrayVal);  
$smarty->assign("branchArray",$branchArray);  
$smarty->display('users.tpl');
?>